﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Survey.DataAccessLayer.Data
{
    public class Accessor<T> where T : class
    {
        private DbContext _db;

        public Accessor(DbContext db)
        {
            _db = db;
        }

        public virtual T Add(T t)
        {
            return _db.Set<T>().Add(t);
        }

        public virtual T Delete(T t)
        {
            return _db.Set<T>().Remove(t);
        }

        public virtual IEnumerable<T> Select(Func<T, bool> predicate)
        {
            return _db.Set<T>().Where(predicate).AsEnumerable();
        }

        public virtual void Update(T t)
        {
            DbEntityEntry entry = _db.Entry<T>(t);
            entry.State = EntityState.Modified;
        }

        public virtual int Save()
        {
            return _db.SaveChanges();
        }
    }
}
